package com.kchouse.homepage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kchouse.R;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class CityListActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private CityListActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;

    private List<String> list = new ArrayList<String>();
    private List<String> listTag = new ArrayList<String>();
    private String cityTags[];
    private ListView lv_city;
    Intent data = new Intent();
    private ListView lv_cityletter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_list);

        mContent = this;
        intiData();
        intiView();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("城市列表");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);


        lv_city = (ListView) findViewById(R.id.lv_city);
        lv_city.setAdapter(new CityListAdapter(mContent, list, listTag));
        lv_city.setOnItemClickListener(this);
        lv_cityletter = (ListView) findViewById(R.id.lv_cityletter);
        lv_cityletter.setAdapter(new MyBaseAdapter(mContent, cityTags));
        lv_cityletter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {

        // TODO Auto-generated method stub
        switch (parent.getId()) {
            case R.id.lv_city:
                boolean isLetter = false;
                for (int i = 0; i < cityTags.length; i++) {
                    if (cityTags[i].equals(listTag.get(position))) {
                        isLetter = true;
                        break;
                    }
                }
                if (isLetter) {
//                    ToastUtils.getInstanc(mContent).showToast(list.get(position));
                    switch (list.get(position)) {
                        case "厦门":

                            data.putExtra("cityname", list.get(position));
                            setResult(0, data);
                            finish();
                            break;
                        case "漳州":
                            data.putExtra("cityname", list.get(position));
                            setResult(0, data);
                            finish();
                            break;
                        default:
                            ToastUtils.getInstanc(mContent).showToast("暂不支持此城市");
                            break;
                    }

                }

                break;
            case R.id.lv_cityletter:
                for (int i = 0; i < listTag.size(); i++) {
                    if (cityTags[position].equals(listTag.get(i))) {
                        lv_city.setSelection(i);
                        break;
                    }
                }
                break;
            default:
                break;
        }
    }


    private void intiData() {

        getData();

    }

    public void getData() {
        String city_name_list[] = mContent.getResources()
                .getStringArray(R.array.city_description_list);
        String city_list_tag[] = mContent.getResources()
                .getStringArray(R.array.city_group_list);
        cityTags = new String[]{"热门", "A", "B", "C", "D", "E", "F", "G", "H", "J",
                "K", "L", "M", "N", "Q", "S", "T", "W", "X", "Y", "Z"};
        int listsize[] = {0, 2, 5, 6, 9, 7, 1, 3, 6, 13, 13, 5, 8, 5, 7, 7,
                10, 6, 11, 7, 11, 9};

        for (int j = 1; j < listsize.length; j++) {
            list.add(cityTags[j - 1]);
            listTag.add(cityTags[j - 1]);
            listsize[j] = listsize[j - 1] + listsize[j];
            for (int i = listsize[j - 1]; i < listsize[j]; i++) {
                list.add(city_name_list[i]);
                // System.out.println(city_list_tag[i]);
                listTag.add(city_list_tag[i]);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    class CityListAdapter extends ArrayAdapter<String> {

        private List<String> listTag = null;

        public CityListAdapter(Context context, List<String> objects, List<String> tags) {
            super(context, 0, objects);
            this.listTag = tags;
        }

        @Override
        public boolean isEnabled(int position) {
            if (listTag.contains(getItem(position))) {
                return false;
            }
            return super.isEnabled(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (listTag.contains(getItem(position))) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.citylist_tag, null);
            } else {
                view = LayoutInflater.from(getContext()).inflate(R.layout.citylist_item, null);
            }
            TextView textView = (TextView) view.findViewById(R.id.group_list_item_text);
            textView.setText(getItem(position));
            return view;
        }
    }

    class MyBaseAdapter extends BaseAdapter {
        private Context context;
        private String[] letter;


        public MyBaseAdapter(Context context, String[] letter) {
            super();
            this.context = context;
            this.letter = letter;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return letter.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return letter[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(context).inflate(R.layout.citylistletter_item, null);
            TextView tv = (TextView) view.findViewById(R.id.group_list_item_text);
            tv.setText(letter[position]);
            return view;
        }
    }
}
